package com.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.demo.entity.Partner;
import com.demo.mapper.PartnerMapper;
import com.demo.model.Telephone;
import com.demo.service.PartnerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PartnerServiceImpl implements PartnerService {
    @Autowired
    private PartnerMapper mapper;

    @Override
    public List<String> path(String roleId) {

        return mapper.path(roleId);
    }

    @Override
    public int reduction(String partnerId) {
        return mapper.reduction(partnerId);
    }

    @Override
    public int findWeixin(String create_user_id, String weixin, Integer partner_type) {
        return mapper.findWeixin(create_user_id, weixin, partner_type);
    }

    @Override
    public int findEmail(String create_user_id, String email, Integer partner_type, String name) {
        return mapper.findEmail(create_user_id, email, partner_type, name);
    }

    @Override
    public List<String> partnerId(String partnerId) {
        return mapper.partnerId(partnerId);
    }

    @Override
    public int updateAll(String name, String sex, String telephone, String weixin, String email, List<Telephone> telephoneList, List<String> part) {
        return mapper.updateAll(name, sex, telephone, weixin, email, telephoneList, part);
    }

    @Override
    public PageInfo<Partner> findByCreatUser(String createUserId, String partnerType, int pageIndex, int PageSize) {
        PageHelper.startPage(pageIndex, PageSize);
        PageInfo<Partner> pageInfo = new PageInfo<>(mapper.findByCreatUser(createUserId, partnerType));
        return pageInfo;
    }

    @Override
    public PageInfo<Partner> findByCity(Integer partnerType, String[] cities, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageInfo<Partner> pageInfo = new PageInfo<>(mapper.findByCity(partnerType, cities));
        return pageInfo;
    }

    @Override
    public int findByOnly(String create_user_id, String telephone, Integer partner_type) {
        return mapper.findByOnly(create_user_id, telephone, partner_type);
    }


    @Override
    public PageInfo<Partner> findAssetInf(String createId, String userId, String company, Integer partnerType, String belong, String[] cities, String orgType, String orgRemark, String typeId, int pageIndex, int PageSize) {
        PageHelper.startPage(pageIndex, PageSize);
        List<Partner> partnerList = mapper.findAssetInf(createId, company, partnerType, belong, cities, orgType, orgRemark, typeId);
        for (Partner partner : partnerList) {
            if (!userId.equals(partner.getCreateUserId())) {
                partner.setName((partner.getName().subSequence(0, 1)) + "**");
                if (partner.getTelephone() != null && !"".equals(partner.getTelephone())) {
                    partner.setTelephone(partner.getTelephone().subSequence(0, 3) + "*******");
                }
                if (partner.getWeixin() != null && !"".equals(partner.getWeixin())) {
                    partner.setWeixin(partner.getWeixin().subSequence(0, 1) + "*******");
                }
                if (partner.getTelephoneList().size() > 0) {
                    List<Telephone> t = JSON.parseArray(JSON.toJSONString(partner.getTelephoneList()),
                            Telephone.class);
                    for (int i = 0; i < t.size(); i++) {
                        t.get(i).setTelephones("******");
                    }
                    partner.setTelephoneList(t);
                }
                partner.setEmail("******@**.com");
                partner.setCompany("********");
            } else {
                partner.setSelf(1);
            }
        }
        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findAssetInf1(String depId, String createId, String company, Integer partnerType, String belong, String[] cities, String orgType, String orgRemark, String typeId, int pageIndex, int PageSize) {
        PageHelper.startPage(pageIndex, PageSize);
        List<Partner> partnerList = mapper.findAssetInfSys(depId, createId, company, partnerType, belong, cities, orgType, orgRemark, typeId);
        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findFundInfo(String createId, String userId, String company, Integer partnerType, String orgType,
                                          String orgRemark, String currency,
                                          String incomeType, String struct,
                                          Integer scaleMin, Integer scaleMax, String deadline,
                                          String[] cities, String typeId,
                                          String typeName, Integer incomeRate, int pageIndex,
                                          int PageSize) {
        PageHelper.startPage(pageIndex, PageSize);


        List<Partner> partnerList = mapper.findFundInfo(createId, company, partnerType, orgType, orgRemark, currency, incomeType, struct, scaleMin, scaleMax, deadline, cities, typeId, typeName, incomeRate);
        for (Partner partner : partnerList) {
            if (!userId.equals(partner.getCreateUserId())) {
                partner.setName((partner.getName().subSequence(0, 1)) + "**");
                if (partner.getTelephone() != null && !"".equals(partner.getTelephone())) {
                    partner.setTelephone(partner.getTelephone().subSequence(0, 3) + "*******");
                }
                if (partner.getWeixin() != null && !"".equals(partner.getWeixin())) {
                    partner.setWeixin(partner.getWeixin().subSequence(0, 1) + "*******");
                }
                if (partner.getTelephoneList().size() > 0) {
                    List<Telephone> t = JSON.parseArray(JSON.toJSONString(partner.getTelephoneList()),
                            Telephone.class);
                    for (int i = 0; i < t.size(); i++) {
                        t.get(i).setTelephones("******");
                    }
                    partner.setTelephoneList(t);
                }
                partner.setEmail("******@**.com");

                partner.setCompany("********");
            } else {
                partner.setSelf(1);
            }
        }

        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findFundInfo1(String depId, String createId, String company, Integer partnerType, String orgType,
                                           String orgRemark, String currency,
                                           String incomeType, String struct,
                                           Integer scaleMin, Integer scaleMax, String deadline,
                                           String[] cities, String typeId,
                                           String typeName, Integer incomeRate, int pageIndex,
                                           int PageSize) {
        PageHelper.startPage(pageIndex, PageSize);
        int a = cities.length;
        if (cities == null) {
            System.out.println();
        } else {
            System.out.println();
        }
        List<Partner> partnerList = mapper.findFundInfoSys(depId, createId, company, partnerType, orgType, orgRemark, currency, incomeType, struct, scaleMin, scaleMax, deadline, cities, typeId, typeName, incomeRate);
        return new PageInfo<>(partnerList);
    }

    @Override
    public PageInfo<Partner> findAll(int pageIndex, int PageSize) {
        PageHelper.startPage(pageIndex, PageSize);
        PageInfo<Partner> pageInfo = new PageInfo<>(mapper.findAll());
        return pageInfo;
    }

    @Override
    public String depId(String userId) {
        return mapper.depId(userId);
    }


    @Override
    public PageInfo<Partner> findByPartType(Integer partnerType, String createId, int pageIndex, int PageSize) {
        PageHelper.startPage(pageIndex, PageSize);
        PageInfo<Partner> pageInfo = new PageInfo<>(mapper.findByPartType(partnerType, createId));
        return pageInfo;
    }

    @Override
    public int add(Partner partner) {
        return mapper.insert(partner);
    }

    //更新数据
    @Override
    public int update(Partner partner) {
        return mapper.update(partner);
    }

    //根据id删除数据
    @Override
    public int remove(String partnerId) {
        return mapper.remove(partnerId);
    }

    //根据id获取数据
    @Override
    public Partner get(String partnerId) {
        return mapper.get(partnerId);
    }
}
